/*
Content: Table G.7
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
**# LOAD ANALYSIS DATASET
********************************************************************************
	
use "$final/finaldata_ieb_main_light.dta", replace

********************************************************************************
**# Table G.7
********************************************************************************			
* Create balance variables 

* Dummies for education level of the individual
tab edu_max, g(education_)

* Get the share of natives as 1- % foreigners
replace share_for_lb_ab_med = 1 -share_for_lb_ab_med

* Firm size in spell before unemployment
cap clonevar ln_firmsize_lb = ln_firmsize


cap log close 

log using "$log/iebbalance_tables_individual_char_did_final.log", replace

cap egen miss =  rowmiss(ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb)

eststo clear
local i=1
foreach out in education_1 education_2 education_3 age frau ysm_atunemp ln_wage_any_lb full_time_lb ln_firmsize_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb {
    eststo mod`i': reghdfe `out' i.treatment_terror_m3y_p90##i.unemp_3m if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & miss == 0, absorb(i.ieb_nat_id#i.year2 i.month#i.year2 i.state) vce(cluster clusterterror_m3y_p90)
    local i=`i'+1
}

// Use estout to create transposed table
estout mod* using "$table/tableG7_final.tex", replace cells("b(fmt(3)) se(fmt(3) par)") keep(1.treatment_terror_m3y_p90#1.unemp_3m) ///
    mlabels("Lower edu" "Middle edu" "High edu" "Age" "Female" "Years since migration" "Ln daily wage" "Full-time employ." "Ln firm size" "Share Germans (> p50)" "Share HS (> p50)" "AKM (> p50)") ///
    collabels("Estimated Coeff." "Standard Error")  style(tex) ///
    prehead("\begin{tabular}{lcc}" "\hline" "Characteristic & Estimated Coeff. & Standard Error \\" "& (1) & (2) \\") ///
    postfoot("\hline" "Observations & \multicolumn{2}{c}{47305} \\" "Origin country FE x Year FE & \multicolumn{2}{c}{Yes} \\" "Year FE x Month FE & \multicolumn{2}{c}{Yes} \\" "State FE & \multicolumn{2}{c}{Yes} \\" "\end{tabular}")
	
	
cap log close 


